#include "mytask.h"

void Task1code( void *pvParameters );
void Task2code( void *pvParameters );

static void imu_task(void *arg);
static void display_task(void *arg);
static void timer_task(void *arg);
static void rgb_task(void *arg);
static void test_task(void *arg);

void mytask_create(void)
{
    WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);//关闭低电压检测,避免无限重启
    //最后一个参数至关重要，决定这个任务创建在哪个核上.PRO_CPU 为 0, APP_CPU 为 1,或者 tskNO_AFFINITY 允许任务在两者上运行.
    xTaskCreatePinnedToCore(Task1code, "Task1", 65536 * 4, NULL, 1, NULL,  0);
    xTaskCreatePinnedToCore(Task2code, "Task2", 4096, NULL, 1, NULL,  1);

    // xTaskCreate(imu_task, "imu_task", 1024 * 2, NULL, configMAX_PRIORITIES - 1, NULL);
    xTaskCreate(display_task, "display_task", 4096 * 2, NULL, configMAX_PRIORITIES - 2, NULL);
    // xTaskCreate(timer_task, "timer_task", 1024 * 2, NULL, configMAX_PRIORITIES - 3, NULL);
    xTaskCreate(rgb_task, "rgb_task", 1024 * 2, NULL, configMAX_PRIORITIES - 4, NULL);
    xTaskCreate(test_task, "test_task", 1024 * 2, NULL, configMAX_PRIORITIES - 5, NULL);
}

void Task1code(void *pvParameters)
{
    while(1)
    {
        ;
    }
}
 
void Task2code(void *pvParameters)
{
    long time1 = millis();
    int flag = 0;
    while(1)
    {
        vTaskDelay(500);
        esp_task_wdt_feed();
        mpu.update(200);
    }
}


static void imu_task(void *arg)
{
    static const char *IMU_TASK_TAG = "imu_task";
    // esp_log_level_set(IMU_TASK_TAG, ESP_LOG_INFO);

    while(1)
    {
        esp_task_wdt_feed();
        vTaskDelay(500 / portTICK_PERIOD_MS);
    }
}

static void display_task(void *arg)
{
    static const char *DISPLAY_TASK_TAG = "display_task";
    // esp_log_level_set(DISPLAY_TASK_TAG, ESP_LOG_INFO);

    while(1)
    {
        esp_task_wdt_feed();
        vTaskDelay(4 / portTICK_PERIOD_MS);
    }
}

static void timer_task(void *arg)
{
    static const char *TIMER_TASK_TAG = "timer_task";
    // esp_log_level_set(TIMER_TASK_TAG, ESP_LOG_INFO);

    while(1)
    {

        esp_task_wdt_feed();
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

static void rgb_task(void *arg)
{
    static const char *RGB_TASK_TAG = "rgb_task";
    //esp_log_level_set(RGB_TASK_TAG, ESP_LOG_INFO);

    while(1)
    {

        esp_task_wdt_feed();
        vTaskDelay(50 / portTICK_PERIOD_MS);
    }
}

static void test_task(void *arg)
{
    static const char *RGB_TASK_TAG = "test_task";
    //esp_log_level_set(RGB_TASK_TAG, ESP_LOG_INFO);

    while(1)
    {

        Serial.println(key_state);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}
